/**
 * @description 产品控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const model = require("../../../model/custManager/vipLevelConfig/vipLevel.MODEL")
const { getFilter } = require("../../../utils/setFilter")
const dayjs = require("dayjs")
// 创建一个内容
const controller_save = async ({
    id = undefined,
    levelName = undefined,
    levelCode = undefined,
    levelIcon = undefined,
    vipDiscount = undefined,
    vipDiscountType = undefined,
    vipDiscountValueType = undefined,
    vipDiscountValue2 = undefined,
    vipDiscountValue2Type = undefined,
    vipDiscountValue3 = undefined,
    vipDiscountValue3Type = undefined,
    vipDiscountValue4 = undefined,
    upgradeCondition = undefined,
    validityPeriod = undefined,
    isDefault = undefined,
    freeShipping = undefined,
    pointMultiplier = undefined,
    description = undefined,

}) => {
    try {
        const params = {

            levelName,
            levelCode,
            levelIcon,
            vipDiscount,
            vipDiscountType,
            vipDiscountValueType,
            vipDiscountValue2,
            vipDiscountValue2Type,
            vipDiscountValue3,
            vipDiscountValue3Type,
            vipDiscountValue4,
            upgradeCondition,
            validityPeriod,
            isDefault,
            freeShipping,
            pointMultiplier,
            description,
        }
        let addRes = null;
        if (!id) {
            // 添加创建
            addRes = await model.create({
                ...params,
                id: randomUUID(),
            })
        } else {
            // 更新修改
            addRes = await model.findOneAndUpdate({ id: id }, {
                $set: {
                    ...params
                }
            }, { new: true })
        }

        if (addRes) {
            const {
                id,
                levelName,
                levelCode,
                levelIcon,
                vipDiscount,
                vipDiscountType,
                vipDiscountValueType,
                vipDiscountValue2,
                vipDiscountValue2Type,
                vipDiscountValue3,
                vipDiscountValue3Type,
                vipDiscountValue4,
                upgradeCondition,
                validityPeriod,
                isDefault,
                freeShipping,
                pointMultiplier,
                description,
            } = addRes



            return {
                code: 0,
                msg: "成功",
                data: {
                    id,
                    levelName,
                    levelCode,
                    levelIcon,
                    vipDiscount,
                    vipDiscountType,
                    vipDiscountValueType,
                    vipDiscountValue2,
                    vipDiscountValue2Type,
                    vipDiscountValue3,
                    vipDiscountValue3Type,
                    vipDiscountValue4,
                    upgradeCondition,
                    validityPeriod,
                    isDefault,
                    freeShipping,
                    pointMultiplier,
                    description,
                }
            }
        }
        return {
            code: 100,
            msg: "失败",
        }
    } catch (error) {
        console.log("error:", error)
        return {
            code: 100,
            msg: "出错了：" + error.code,
        }
    }
}

// 查询列表 
const controller_find = async ({
    id = undefined,
    levelName = undefined,
    current,
    pageSize,
}) => {

    try {
        let params = {
            id,
            levelName,
            deleteFlag: false
        }
        filter = getFilter(params)
        const list = await model.find(filter).sort({ sort: 1 }).skip((current - 1) * pageSize).limit(pageSize)
        const totalCount = await model.countDocuments(filter)
        return {
            code: 0,
            msg: "成功",
            data: {
                list,
                totalCount
            }
        }
        // /**
        //  * @description 聚合查询
        //  * 
        //  */
        // const queryList = await model.aggregate([
        //     {
        //         $match: {
        //             ...filter,
        //             deleteFlag: false
        //         }
        //     },
        //     {
        //         $lookup: {
        //             from: "main_tab_skus",
        //             localField: "id",
        //             foreignField: "productId",
        //             as: "skus"
        //         },
        //         $lookup: {
        //             from: "correlation_product_categories",
        //             localField: "categoryId",
        //             foreignField: "productId",
        //             as: "cateId"
        //         }
        //     }
        // ])
        // return queryList;
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障",
        }
    }
}


/**@description 根据id查详情 */
const controller_get = async ({ id, title }) => {
    try {
        const res = await model.findOne({
            id
        })
        if (res) {
            const { id,
                levelName,
                levelCode,
                levelIcon,
                vipDiscount,
                vipDiscountType,
                vipDiscountValueType,
                vipDiscountValue2,
                vipDiscountValue2Type,
                vipDiscountValue3,
                vipDiscountValue3Type,
                vipDiscountValue4,
                upgradeCondition,
                validityPeriod,
                isDefault,
                freeShipping,
                pointMultiplier,
                description, } = res;
            return {
                code: 0,
                msg: "查询成功",
                data: {
                    id,
                    levelName,
                    levelCode,
                    levelIcon,
                    vipDiscount,
                    vipDiscountType,
                    vipDiscountValueType,
                    vipDiscountValue2,
                    vipDiscountValue2Type,
                    vipDiscountValue3,
                    vipDiscountValue3Type,
                    vipDiscountValue4,
                    upgradeCondition,
                    validityPeriod,
                    isDefault,
                    freeShipping,
                    pointMultiplier,
                    description,
                }
            }
        } else {
            return {
                code: 100,
                msg: "未找到对应数据"
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障",
        }
    }
}

// 删除
const controller_delete = async ({ ids = [], id = undefined }) => {
    try {
        //物理删除
        // const res = await model.deleteMany({
        //     id: { $in: ids }
        // });
        // 逻辑删除
        const res = await model.findOneAndUpdate({ id: id }, {
            $set: {
                deleteFlag: true
            }
        }, { new: true })

        if (res) {
            return {
                code: 0,
                msg: `删除成功`
            }
        } else {
            return {
                code: 100,
                msg: `删除失败`
            }
        }
    } catch (error) {
        console.log("删除控制器出错", error)
        return {
            code: 100,
            msg: "服务器故障",
        }
    }

}
module.exports = {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
}